package com.example.usb.dao.util;

import com.example.usb.MyApplication;
import com.example.usb.dao.DaoManager;
import com.example.usb.dao.entity.FaceInfoBean;
import com.example.usb.dao.entity.PhotoPicBean;
import com.example.usb.greendao.PhotoPicBeanDao;
import com.lib.uvccamera.utils.DebugLog;

import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.List;

import static com.example.usb.dao.DbConstant.PIC_NO_DETECTION;

/**
 * Created Date 2019/2/21
 *
 * @author liangkun
 * description：图片数据库处理类
 */
public class PhotoPicDaoUtil {
    private static final String TAG = "HscPicDaoUtil";
    private static PhotoPicDaoUtil instance;

    /**
     * 单例模式
     *
     * @return 单例
     */
    public static PhotoPicDaoUtil getInstance() {
        if (instance == null) {
            synchronized (PhotoPicDaoUtil.class) {
                if (instance == null) {
                    instance = new PhotoPicDaoUtil();
                }
            }
        }
        return instance;
    }

    /**
     * 根据md5删除图片
     * @param md5 唯一值
     */
    public void deletePicByMd5(String md5){
        QueryBuilder<PhotoPicBean> queryBuilder = getPhotoPicDao().queryBuilder();
        PhotoPicBean hscPicBean = queryBuilder.where(PhotoPicBeanDao.Properties.Md5.eq(md5)).build().unique();
        getPhotoPicDao().delete(hscPicBean);
    }

    /**
     * 获取图片dao
     *
     * @return dao管理器
     */
    public PhotoPicBeanDao getPhotoPicDao() {
        return DaoManager.getInstance(MyApplication.getContext()).getDaoSession().getPhotoPicBeanDao();
    }

    /**
     * 插入图片表数据
     *
     * @param hscPicBean 图片表数据
     */
    public void insertPicBean(PhotoPicBean hscPicBean) {
        long insert = getPhotoPicDao().insert(hscPicBean);
    }

    /**
     * 通过md5查询
     *
     * @param md5 照片唯一标识
     * @return 该MD5下的人脸数据
     */
    public List<FaceInfoBean> getFaceListByMd5(String md5) {
        QueryBuilder<PhotoPicBean> queryBuilder = getPhotoPicDao().queryBuilder();
        PhotoPicBean hscPicBean = queryBuilder.where(PhotoPicBeanDao.Properties.Md5.eq(md5)).build().unique();
        //查看MD5下的所有数据
        if (null != hscPicBean) {
            List<FaceInfoBean> faceList = hscPicBean.getFaceList();
            return faceList;
        }
        return null;
    }

    /**
     * 获取没有人脸检测的bean
     *
     * @return 数据
     */
    public List<PhotoPicBean> getNotFaceDetection() {
        QueryBuilder<PhotoPicBean> hscPicBeanQueryBuilder = getPhotoPicDao().queryBuilder();
        WhereCondition whereCondition = PhotoPicBeanDao.Properties.Detection.eq(PIC_NO_DETECTION);
        List<PhotoPicBean> list = hscPicBeanQueryBuilder.where(whereCondition).limit(1).list();
        return list;
    }

    /**
     * 查询没有检测的图片
     *
     * @return 没有检测的图片
     */
    public List<PhotoPicBean> findNotDetectionCount() {
        QueryBuilder<PhotoPicBean> hscPicBeanQueryBuilder = getPhotoPicDao().queryBuilder();
        WhereCondition whereCondition = PhotoPicBeanDao.Properties.Detection.eq(PIC_NO_DETECTION);
        List<PhotoPicBean> picBeanLists = hscPicBeanQueryBuilder.where(whereCondition).build().list();
        return picBeanLists;


    }

    /**
     * 根据ai返回的数据返回多少张人脸
     *
     * @param hscPicBean 图片bean
     * @param faceNum    人脸数
     * @param detection  是否检测 0未检测 1 已检测
     */
    public void updataPicFaceNum(PhotoPicBean hscPicBean, int faceNum, String detection) {
        hscPicBean.setFigureCount(String.valueOf(faceNum));
        hscPicBean.setDetection(detection);
        getPhotoPicDao().update(hscPicBean);
    }

    /**
     * 更新图片的压缩路径
     *
     * @param md5               唯一标识
     * @param compresssFilePath 压缩路径
     */
    public void updatePicCompressFile(String md5, String compresssFilePath) {
        QueryBuilder<PhotoPicBean> queryBuilder = getPhotoPicDao().queryBuilder();
        PhotoPicBean hscPicBean = queryBuilder.where(PhotoPicBeanDao.Properties.Md5.eq(md5)).build().unique();
        getPhotoPicDao().update(hscPicBean);
    }



}
